package com.elong.qadev.dao.impl;

import java.util.ArrayList;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.elong.qadev.dao.CaseDaoI;
import com.elong.qadev.entity.CaseNode;
/**
 * 
* @ClassName: CaseDaoImpl
* @Description: TODO(用例节点数据交互DAO)
* @author kangchen
* @date 2015年9月28日
*
 */
@Repository("caseDao")
public class CaseDaoImpl implements CaseDaoI {
	Logger logger = Logger.getLogger(CaseDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public ArrayList<CaseNode> getAllHttpCaseNodes() {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				CaseNode.class);
		c.add(Restrictions.eq("caseType", "HTTP"));
		@SuppressWarnings("unchecked")
		ArrayList<CaseNode> al = (ArrayList<CaseNode>) c.list();
		
		CaseNode root = new CaseNode();
		root.setId(0);
		root.setCaseType("/");
		root.setIsParent(true);
		root.setName("HTTP用例目录");
		root.setOpen(true);
		root.setpId("0");
		al.add(root);
//		ArrayList<CaseNode> al = (ArrayList<CaseNode>) sessionFactory.getCurrentSession()
//				.createQuery("from CaseNode").list();
		logger.debug("获取用例集合展示：" + al.toString());
		return al;
	}

	@Override
	public CaseNode save(CaseNode cn) {
		sessionFactory.getCurrentSession().save(cn);
		return cn;
	}

	@Override
	public boolean delete(int id) {
		CaseNode cn = new CaseNode();
		cn.setId(id);
		sessionFactory.getCurrentSession().delete(cn);
//		deleteByPid(id);
		return true;
	}

	@Override
	public boolean update(int id, String name) {
		CaseNode cn = (CaseNode) sessionFactory.getCurrentSession().get(CaseNode.class, id);
		cn.setName(name);
		sessionFactory.getCurrentSession().update(cn);
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<CaseNode> getAllSubCases() {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				CaseNode.class);
		c.add(Restrictions.eq("isParent", false));
		ArrayList<CaseNode> cn = null;
		if(c.list().size()!=0&&c.list() != null){
			cn = (ArrayList<CaseNode>) c.list();
		}
		if (cn != null) {
			return cn;
		}
		return cn;
	}

	@Override
	public CaseNode getCase(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				CaseNode.class);
		c.add(Restrictions.eq("id", caseId));
		if(c.list().isEmpty()){
			return null;
		}
		CaseNode e = (CaseNode) c.list().get(0);
		return e;
	}

	@Override
	public ArrayList<CaseNode> getWebuiCaseNodes() {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				CaseNode.class);
		c.add(Restrictions.eq("caseType", "WEBUI"));
		@SuppressWarnings("unchecked")
		ArrayList<CaseNode> al = (ArrayList<CaseNode>) c.list();
		
		CaseNode root = new CaseNode();
		root.setId(0);
		root.setCaseType("/");
		root.setIsParent(true);
		root.setName("WEBUI用例目录");
		root.setOpen(true);
		root.setpId("0");
		al.add(root);
		logger.debug("获取用例集合展示：" + al.toString());
		return al;
	}
}
